package com.flying.invest.job;

import java.util.List;

import com.flying.common.log.Logger;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.Service;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.service.AbstractService;
import com.flying.invest.strategy.model.Strategy;
import com.flying.invest.strategy.model.Strategy.Status;
import com.flying.invest.strategy.service.StrategyService;

@Service(value="StrategyJob", desc="")
public class StrategyJob extends AbstractService {
	private final static Logger log = Logger.getLogger(StrategyJob.class); 
	private StrategyService ss;
	
	public void setStrategyService(StrategyService s) {
		this.ss = s;
	}
	
	public ActionResult run() {
		List<Strategy> list = this.ss.findAll(null, null, null, Status.PROCESSING.value(), 0, Integer.MAX_VALUE).getRows();
		list.stream().forEach(s -> {
			try {
				this.ss.flushDetails(s.getStrategyId());
				this.ss.executeStrategy(s.getStrategyId());
			}catch (Exception e) {
				log.error(s.getName()+" execute error!", e);
			}
		});
		return ModelFactory.create(ActionResult.class, Codes.CODE, Codes.SUCCESS);
	}
}
